package com.qf.dao.impl;

import com.qf.dao.Credit_CardDao;
import com.qf.entity.Credit_Card;
import com.qf.entity.User;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class Credit_CardDaoImpl implements Credit_CardDao {

    private QueryRunner queryRunner = new QueryRunner(JdbcUtils.ds);

    @Override
    public List<Credit_Card> selectCCAll() {
        try {
            return queryRunner.query("select * from t_credit_card",new BeanListHandler<>(Credit_Card.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User selectUIDrByRegister(String name, String num, String phone) {
        String sql = "select * from t_user where username=? and id_num=? and phone=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<>(User.class), name, num, phone);
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertCC(Credit_Card card) {
        String sql = "insert into t_credit_card values(?,?,?,null ,?,?,?)";
        try {
            return queryRunner.update(sql, card.getId(), card.getLimit(), card.getUid(), card.getInterest(), card.getAmount(),card.getRepayment());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Credit_Card selectCCByID(String id) {
        try {
            return queryRunner.query("select * from t_credit_card where id = ?",new BeanHandler<>(Credit_Card.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Credit_Card> selectCCByUID(String uid) {
        try {
            return queryRunner.query("select * from t_credit_card where uid = ?",new BeanListHandler<>(Credit_Card.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateAmount(Double num, String id) {
        String sql = "UPDATE t_credit_card SET amount = (amount - ?) where id = ?";
        try {
            return queryRunner.update(sql, num, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int jiaqian(double num, String cid) {
        String sql = "UPDATE t_credit_card SET amount = (amount + ?) where id = ?";
        try {
            return queryRunner.update(sql,num,cid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateRepaymentByID(double repayment, String cid) {
        String sql = "UPDATE t_credit_card SET Repayment = ? where id = ?";
        try {
            return queryRunner.update(sql, repayment, cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
